import fs from 'fs-extra';
import path from 'path';

export class Extractor {

    private logStack = `PuertsStaticWrap.Log_Wrap:F_log(IntPtr, IntPtr, IntPtr, Int32, Int64)
Puerts.PuertsDLL:ExecuteModule(IntPtr, String, String)
Puerts.JsEnv:ExecuteModule(String)
JVM:Init(String, String, String, Int32)
System.Action\`1:Invoke(T)
ResLoader:Update()
    
(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)`;

    public async prettify(raw: string): Promise<void> {
        const content = await fs.readFile(raw, 'utf-8');
        const lines = content.split(/\r?\n/);
        const unityLines: string[] = [];
        for (let i = 0, len = lines.length; i < len; i++) {
            if (i > 0 && (lines[i] == 'PuertsStaticWrap.Log_Wrap:F_log(IntPtr, IntPtr, IntPtr, Int32, Int64)' || lines[i] == 'UnityEngine.StackTraceUtility:ExtractStackTrace ()')) {
                unityLines.push(lines[i - 1]);
            }
        }
        const newContent = unityLines.join('\n');
        const ext = path.extname(raw);
        let output: string;
        if (ext != '') {
            output = raw.replace(ext, '.unity' + ext);
        } else {
            output = raw + '.unity';
        }
        await fs.writeFile(output, newContent, 'utf-8');
    }
}
